package com.eternal.org.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.eternal.Exception.ResultEnum;
import com.eternal.Exception.ResultException;
import com.eternal.org.entity.OrganizeInfo;
import com.eternal.org.dao.OrganizeInfoMapper;
import com.eternal.org.service.OrganizeInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.eternal.permission.entity.PermissionInfo;
import com.eternal.permission.entity.enums.PTypeEnum;
import com.eternal.util.tree.BuildTree;
import com.eternal.util.tree.entity.Tree;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;

/**
 * <p>
 * 基础组织机构表 服务实现类
 * </p>
 *
 * @author Eternal
 * @since 2018-10-28
 */
@Service
@Primary
@Transactional
public class OrganizeInfoServiceImpl extends ServiceImpl<OrganizeInfoMapper, OrganizeInfo> implements OrganizeInfoService {

    @Override
    public List<Tree<OrganizeInfo>> getOrganizeTree() {
        Wrapper<OrganizeInfo> wrapper = new EntityWrapper<>();
        wrapper.where("is_available={0}", "1");
        List<OrganizeInfo> organizeInfos = baseMapper.selectList(wrapper);

        List<Tree<OrganizeInfo>> trees = new ArrayList<>();

        for (OrganizeInfo organizeInfo : organizeInfos) {
            Tree<OrganizeInfo> tree = new Tree<>();
            tree.setId(organizeInfo.getOrgId());
            tree.setTitle(organizeInfo.getOrgName());
            tree.setParentId(organizeInfo.getOrgParentId());
            trees.add(tree);
        }
        List<Tree<OrganizeInfo>> build = BuildTree.build(trees);
        return build;
    }

    @Override
    public List<Tree<OrganizeInfo>> getOrganizeTree(String orgId) {
        Wrapper<OrganizeInfo> wrapper = new EntityWrapper<>();
        wrapper.where("is_available={0}", "1");
        List<OrganizeInfo> organizeInfos = baseMapper.selectList(wrapper);


        List<Tree<OrganizeInfo>> trees = new ArrayList<>();

        for (OrganizeInfo organizeInfo : organizeInfos) {
            Tree<OrganizeInfo> tree = new Tree<>();
            tree.setId(organizeInfo.getOrgId());
            tree.setTitle(organizeInfo.getOrgName());
            tree.setParentId(organizeInfo.getOrgParentId());
            if (organizeInfo.getOrgId().equals(orgId)) {
                tree.setExpand(true);
            }
            trees.add(tree);
        }

        List<Tree<OrganizeInfo>> expandTrees = BuildTree.expandParentTree(trees, orgId);

        List<Tree<OrganizeInfo>> build = BuildTree.build(expandTrees);
        return build;
    }

    @Override
    public List<Tree<OrganizeInfo>> getOrganizeTreeLikeName(String orgName) {
        List<String> list = baseMapper.selectOrganizeLikeOrgName(orgName);
        List<OrganizeInfo> organizeInfos=new ArrayList<>();
        try {
            organizeInfos = baseMapper.selectOrganizeTreeLikeName(list);
        }catch (Exception e){

        }

        List<Tree<OrganizeInfo>> trees = new ArrayList<>();
        List<String> parentStr = new ArrayList<>();
        for (OrganizeInfo organizeInfo : organizeInfos) {
            parentStr.add(organizeInfo.getOrgId());
        }

        for (OrganizeInfo organizeInfo : organizeInfos) {
            Tree<OrganizeInfo> tree = new Tree<>();
            tree.setId(organizeInfo.getOrgId());
            tree.setTitle(organizeInfo.getOrgName());
            if (parentStr.indexOf(organizeInfo.getOrgParentId()) == -1) {
                tree.setParentId("");
            } else {
                tree.setParentId(organizeInfo.getOrgParentId());
            }
            trees.add(tree);
        }

        List<Tree<OrganizeInfo>> build = BuildTree.build(trees);

        return build;
    }

    @Override
    public boolean saveOrgnizeInfo(OrganizeInfo organizeInfo) {
        boolean insert = organizeInfo.insert();
        return insert;
    }

    @Override
    public boolean importExcelOrg(MultipartFile file) {
        if (file == null) {
            return false;
        }
        ImportParams params = new ImportParams();
        try {
            List<OrganizeInfo> list = ExcelImportUtil.importExcel(file.getInputStream(), OrganizeInfo.class, params);

            for (OrganizeInfo organizeInfo : list) {
                boolean insert = organizeInfo.insertOrUpdate();
            }
        } catch (NoSuchElementException e) {
            throw new ResultException(ResultEnum.IMPORT_EXCEL_NULL_ERROR);
        } catch (IOException e) {
            throw new ResultException(ResultEnum.READ_EXCEL_ERROR);
        } catch (Exception e) {
            throw new ResultException(ResultEnum.IMPORT_ORGANIZE_ERROR);
        }

        return true;
    }



}
